<?php

namespace App\Admin\Actions\Form;

use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use Dcat\Admin\Admin;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\SecondDataImport;

class DealUpload extends Form
{

    public function __construct($randKey = null) 
    {
        $this->randKey = $randKey;

        parent::__construct();
    }

    // 处理表单提交请求
    public function handle(array $input)
    {
        $path = public_path('uploads/');
        $file = $path.$input['csv'];

        Excel::import(new SecondDataImport, $file);
        unlink($file);

        return $this->response()->success('解析完成.');
    }

    // 构建表单
    public function form()
    {
        $this->confirm('您确定要解析该文件吗', '');
        $this->file('csv', '文件')->uniqueName()->autoUpload()->accept('csv,xls,xlsx')->required();
    }
}